package cn.com.bluemoon.daps.model.utils.factory.impl.table;

import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.sql.SqlScriptUtils;
import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.ModelTableInfoDto;
import cn.com.bluemoon.daps.model.dto.SubordinateKeyDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.model.utils.SqlUtils;
import cn.com.bluemoon.daps.model.utils.factory.TableScriptFactory;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.AbsFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.MysqlFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.PostgreFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.AbsKeyScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.MysqlKeyScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.PostgreKeyScript;
import com.alibaba.druid.DbType;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author karry
 * @Data 2021/9/3
 * @Time:14:08
 */
public class MysqlTableScript extends AbsTableScript {

    public final static DbType MYSQL = DbType.mysql;

    public MysqlTableScript() {
        super(MYSQL);
    }

    @Override
    public String createTableScript(ModelTableInfoDto dto, List<SubordinateKeyDto> keys, List<DapDataModelTablePartition> partitions) {
        // 自增默认值
        Optional<Long> autoIncrDefaultValue = dto.getFieldList().stream().filter(f -> f.getAutoIncrDefaultValue() != null && !SqlUtils.isNull(f.getAutoIncrement()))
                .max(Comparator.comparing(DapDataModelTableField::getFieldRank)).map(DapDataModelTableField::getAutoIncrDefaultValue);
        String fieldScript = dto.getFieldList().isEmpty() ? "" : mysqlFieldScript.createFieldScript(dto.getFieldList());
        String keyScript = keys.isEmpty() ? "" : mysqlKeyScript.createKeyScript(keys);
        StringBuffer buffer = new StringBuffer().append(fieldScript).append(keyScript);
        String fieldAndKeyScript = buffer.length() > 0 ? buffer.deleteCharAt(buffer.length() - 1).toString() : "";
        String format = String.format("create table %s (%s)", dto.getEngName(), fieldAndKeyScript);
        String tableComment = String.format("'%s'", SqlScriptUtils.formatComment(dto.getName()));
        String end = String.format("%s COMMENT %s", autoIncrDefaultValue.isPresent() ? "AUTO_INCREMENT=" + autoIncrDefaultValue.get() : "", tableComment);
        return String.format("%s%s;", format, end);
    }

    @Override
    public void modifyTableScript(DapDataModelTableDto newDto, DapDataModelTableDto oldDto,
                                  String schema, Map<String, String> map) {
    }

    @Override
    public void deleteTableScript(String tableName, String schema, Map<String, String> map) {
    }

}
